package com.solomiao.demo.multi.thread.demo10;

import java.util.concurrent.ExecutionException;

/**
 * @desc 理解线程的各个状态
 * */
public class MainDemo10 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        {
            System.out.println("——————————————————————————————————————————————————————————");
            // 状态链路：NEW -> RUNNABLE -> TERMINATED
            ThreadStatusDemo1 runnable1 = new ThreadStatusDemo1();
            Thread thread1 = new Thread(runnable1);
            // 此时线程的状态为NEW
            System.out.println("线程状态：" + thread1.getState());
            // 调用start方法后，线程开始运行，状态为RUNNABLE
            thread1.start();
            System.out.println("线程状态：" + thread1.getState());
            thread1.join();
            // 调用join方法后，线程的状态为TERMINATED
            System.out.println("线程状态：" + thread1.getState());
        }
        {
            System.out.println("——————————————————————————————————————————————————————————");
            // 状态链路：NEW -> RUNNABLE -> TIMED_WAITING -> TERMINATED
            ThreadStatusDemo2 runnable2 = new ThreadStatusDemo2();
            Thread thread2 = new Thread(runnable2);
            // 此时线程的状态为NEW
            System.out.println("线程状态：" + thread2.getState());
            // 调用start方法后，线程开始运行，状态为RUNNABLE
            thread2.start();
            System.out.println("线程状态：" + thread2.getState());
            // 子线程休眠20秒，同时主线程休眠2秒后，打印子线程状态，状态为TIMED_WAITING
            Thread.sleep(2000);
            System.out.println("线程状态：" + thread2.getState());
            thread2.join();
            // 调用join方法后，线程的状态为TERMINATED
            System.out.println("线程状态：" + thread2.getState());
        }
    }


}
